﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration.Install;
using System.ComponentModel;
using System.Security.Permissions;
using System.IO;
using System.Collections;
using System.Reflection;
using System.Diagnostics;

namespace Extensil.PostInstaller
{
    [RunInstaller(true)]
    public class PluginInstaller : Installer
    {
        [SecurityPermission(SecurityAction.Demand)]
        private void WriteFile(string message)
        {
            string logPath = Path.GetDirectoryName(Context.Parameters["InstallerPath"]) + "\\install.log";
            try
            {
                File.AppendAllText(logPath, "\r\n" + message);
            }
            catch(Exception esc)
            {
                File.AppendAllText(logPath, esc.Message);
            }
        }
        [SecurityPermission(SecurityAction.Demand)]
        protected override void OnCommitted(IDictionary savedState)
        {
            InstallPlugins();           
            base.OnCommitted(savedState);
        }
        void InstallPlugins()
        {
            WriteFile("Installing Plugins.");
            string pluginPath = Path.GetDirectoryName(Context.Parameters["InstallerPath"]) + "\\Plugin";
            if(!Directory.Exists(pluginPath))
            {
                WriteFile("No plugins available.");
                return;
            }
            DirectoryInfo di = new DirectoryInfo(pluginPath);
            FileInfo[] fInfos = di.GetFiles("*.zip");
            if(fInfos.Length == 0)
            {
                WriteFile("No plugins available.");
                return;
            }
            foreach(FileInfo fInfo in fInfos)
            {
                try
                {
                    WriteFile("Installing " + fInfo.FullName + ".");
                    Process catInstaller = new Process();
                    FileInfo fInst = new FileInfo(Context.Parameters["Targetdir"] + @"xtensil.exe");
                    if(!fInst.Exists)
                    {
                        WriteFile(fInst.FullName + " could not be found.");
                        break;
                    }
                    catInstaller.StartInfo.FileName = fInst.FullName;
                    catInstaller.StartInfo.Arguments = "-pluginPath=\"" + fInfo.FullName + "\"";
                    catInstaller.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                    catInstaller.StartInfo.UseShellExecute = false;
                    catInstaller.StartInfo.RedirectStandardOutput = true;
                    catInstaller.Start();

                    WriteFile(catInstaller.StandardOutput.ReadToEnd());

                    catInstaller.WaitForExit();
                }
                catch(Exception exc)
                {
                    WriteFile(exc.Message + "\r\n\t" + exc.StackTrace);
                }
            }
        }
        public void ReportStatus(int progress, string message)
        {
            WriteFile(message);
        }
        
    }
}
